package com.bit.dreamer;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import android.app.ActionBar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class DreameditActivity extends Activity implements OnClickListener {
	public static final int PHOTO = 1;
	Button bt_insert;
	int mYear, mMonth, mDay, sel_bt;
	TextView tv_insert_date;
	EditText edt_insert_title, edt_insert_content;
	ImageView img_insert;
	Bitmap selPhoto;
	DreamHelper helper;
	SQLiteDatabase db;
	Cursor rs;
	Bitmap bitmap;
	byte[] bArr;
	int _id;
	Uri uri;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dreamedit);
		
		ActionBar actionbar = getActionBar();
		SharedPreferences sf = getSharedPreferences("name", MODE_PRIVATE);
		String nName = sf.getString("name", "");
		actionbar.setTitle(nName + "님 안녕하세요");
		actionbar.setDisplayShowHomeEnabled(false);
		
		bt_insert = (Button)findViewById(R.id.bt_insert);
		tv_insert_date = (TextView)findViewById(R.id.tv_insert_date);
		edt_insert_title = (EditText)findViewById(R.id.edt_insert_title);
		edt_insert_content = (EditText)findViewById(R.id.edt_insert_content);
		img_insert = (ImageView)findViewById(R.id.img_insert);
		
		bt_insert.setOnClickListener(this);
		tv_insert_date.setOnClickListener(this);
		img_insert.setOnClickListener(this);
		
		Calendar cal = new GregorianCalendar();
		mYear = cal.get(Calendar.YEAR);
		mMonth = cal.get(Calendar.MONTH);
		mDay = cal.get(Calendar.DAY_OF_MONTH);
		
		_id = getIntent().getIntExtra("_id", 0);
		sel_bt = getIntent().getIntExtra("sel", 0);
		
		
		helper = new DreamHelper(this);
		db = helper.getReadableDatabase();
		switch (sel_bt) {
		case 1:
			rs = db.rawQuery("SELECT _id, title, dreamdate, img, memo FROM D_do_tb WHERE _id = '" + _id + "'", null);
			break;
			
		case 2:
			rs = db.rawQuery("SELECT _id, title, dreamdate, img, memo FROM D_have_tb WHERE _id = '" + _id + "'", null);
			break;
			
		case 3:
			rs = db.rawQuery("SELECT _id, title, dreamdate, img, memo FROM D_become_tb WHERE _id = '" + _id + "'", null);
			break;
		}
		
		while(rs.moveToNext()){
//			bArr = rs.getBlob(3);
//			Bitmap bit = BitmapFactory.decodeByteArray(bArr , 0 , bArr.length);
//			img_insert.setImageBitmap(bit);
			
			uri = Uri.parse(rs.getString(3));
			
			Bitmap bitmap;
			Bitmap resized = null;
			try {
				bitmap = Images.Media.getBitmap(getContentResolver(), Uri.parse(rs.getString(3)));
				int height = bitmap.getHeight();
				int width = bitmap.getWidth();
				if(height > 2048) {
					while (height > 2048) {
						resized = Bitmap.createScaledBitmap(bitmap, (width * 2048) / height, 2048, true);
						height = resized.getHeight();
						width = resized.getWidth();
						img_insert.setImageBitmap(resized);
					}
				} else {
					img_insert.setImageBitmap(bitmap);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			
			
			
			
			
			
			
			
			
//			img_insert.setImageURI(Uri.parse(rs.getString(3)));
			edt_insert_title.setText(rs.getString(1));
			tv_insert_date.setText(rs.getString(2));
			edt_insert_content.setText(rs.getString(4));
		}
		
		helper.close();
	}//onCreate

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			
		case R.id.tv_insert_date :
			new DatePickerDialog(DreameditActivity.this, mDateSetListener, mYear, mMonth, mDay).show();
			break;
			
		case R.id.img_insert :
			Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
			intent.setType("image/*");	
			try {
				startActivityForResult(intent, PHOTO);
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
			
		case R.id.bt_insert : 
			db = helper.getWritableDatabase();
			ContentValues values = new ContentValues(); 
			values.put("title", edt_insert_title.getText().toString());
			values.put("dreamdate", tv_insert_date.getText().toString());
			values.put("img", uri.toString());
			values.put("memo", edt_insert_content.getText().toString());
			
			switch (sel_bt) {
			case 1:
				db.update("D_do_tb", values, "_id = '" + _id + "'", null);
				break;
				
			case 2:
				db.update("D_have_tb", values, "_id = '" + _id + "'", null);
				break;
				
			case 3:
				db.update("D_become_tb", values, "_id = '" + _id + "'", null);
				break;
			}
			
			helper.close();
			System.gc();
			Intent intent2 = new Intent(getApplicationContext(), DreamlistActivity.class);
			intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			intent2.putExtra("bt_select", sel_bt);
//			img_insert.setImageBitmap(null);
			startActivity(intent2);
			break;
		}//onClick
		
	}
	
	DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear;
			mDay = dayOfMonth;
			tv_insert_date.setText(String.format("%d-%d-%d", mYear, mMonth+1, mDay));
		}
	};//mDateSetListener

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == PHOTO && resultCode == RESULT_OK) {
			uri = data.getData();
			Bitmap bitmap;
			Bitmap resized = null;
			try {
				bitmap = Images.Media.getBitmap(getContentResolver(), uri);
				int height = bitmap.getHeight();
				int width = bitmap.getWidth();
				if(height > 2048) {
					while (height > 2048) {
						resized = Bitmap.createScaledBitmap(bitmap, (width * 2048) / height, 2048, true);
						height = resized.getHeight();
						width = resized.getWidth();
						img_insert.setImageBitmap(resized);
					}
				} else {
					img_insert.setImageBitmap(bitmap);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
//			try {
//				bitmap = Images.Media.getBitmap(getContentResolver(), uri);
//				int height = bitmap.getHeight();
//				int width = bitmap.getWidth();
//				Bitmap resized = null;
//				while (height > 240) {
//					resized = Bitmap.createScaledBitmap(bitmap, (width * 240) / height, 240, true);
//					height = resized.getHeight();
//					width = resized.getWidth();
//				}
//				img_insert.setImageBitmap(resized);
//				ByteArrayOutputStream stream = new ByteArrayOutputStream();
//				resized.compress(CompressFormat.JPEG , 100 , stream);
//				bArr = stream.toByteArray();
//			} catch (FileNotFoundException e) {
//				e.printStackTrace();
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
		}
	}//onActivityResult

	public void onDestory() {
		db.close();
	}
}
